package com.klhappy.houyuan.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.klhappy.houyuan.R;
import com.klhappy.houyuan.adapter.SearchGridAdapter;
import com.klhappy.houyuan.adapter.SearchListAdapter;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.bean.SearchBean.Records;
import com.klhappy.houyuan.bean.SearchBean.SearchBean;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.HouYuanModle;
import com.klhappy.houyuan.utils.JsonCode;
import com.klhappy.houyuan.view.AbsActivity;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by MrPu on 15/12/21.
 */
public class SearchProductActivity extends AbsActivity implements TextWatcher, CallBackInterModle {
    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.lv_good)
    ListView lv_good;
    @InjectView(R.id.gv_good)
    GridView gv_good;
    @InjectView(R.id.input_edit)
    EditText input_edit;
    @InjectView(R.id.iv_clean)
    ImageView iv_clean;

    @OnClick(R.id.click_ra)
    public void click_ra() {
        finish();
    }

    @OnClick(R.id.input_edit)
    public void input_edit() {
        input_edit.setHint("");
        input_edit.setCursorVisible(true);
        lv_good.setVisibility(View.VISIBLE);
        gv_good.setVisibility(View.GONE);
    }

    @OnClick(R.id.ll_search)
    public void ll_search() {
        input_edit.setText("");
        iv_clean.setBackgroundResource(R.drawable.serch_img);
    }

    private HouYuanModle modle;
    private SearchBean searchBean;
    private List<Records> recordses;
    private SearchListAdapter adapter;
    private SearchGridAdapter gridAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
        ButterKnife.inject(this);
        title.setText("搜索商品");

        modle = new HouYuanModle(this);
        modle.setCallBackInterModle(this);

        input_edit.addTextChangedListener(this);
        lv_good.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                gv_good.setVisibility(View.VISIBLE);
                lv_good.setVisibility(View.GONE);
                gridAdapter = new SearchGridAdapter(SearchProductActivity.this ,recordses.get(position));
                gv_good.setAdapter(gridAdapter);

            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        iv_clean.setBackgroundResource(R.drawable.qingchu);
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (!s.toString().equals("")){
            modle.getSearch(s.toString());
        }
    }

    @Override
    public void CallBackFromModle(String url, BaseJsonBean o) {
        if (url.equals(UrlConstant.SEARCH_URL)) {
            Gson gson = new Gson();
            Log.e("this" ,gson.toJson(o));
            if (o.getCode() == 200){
                searchBean = JsonCode.getClass(o.getEntity(), SearchBean.class);
                recordses = JsonCode.getClassList(searchBean.getRecords(), Records.class);
                if (recordses.size() != 0){
                    setList();
                }else {
                    showToastx("没有搜索到商品");
                }
            }else {
                showToastx(o.getMsg());
            }
        }
    }

    private void setList(){
        adapter = new SearchListAdapter(this ,recordses);
        lv_good.setAdapter(adapter);
    }
}
